package com.xuyiwei.xblog.modular.article.controller;

import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.util.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.xuyiwei.xblog.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.xuyiwei.xblog.business.article.model.ArticleLove;
import com.xuyiwei.xblog.business.article.service.IArticleLoveService;
import java.util.List;

/**
 * 文章喜欢控制器
 *
 * @author fengshuonan
 * @Date 2018-10-29 17:08:00
 */
@Controller
@RequestMapping("/article/articleLove")
public class ArticleLoveController extends BaseController {

    private String PREFIX = "/article/articleLove/";

    @Autowired
    private IArticleLoveService articleLoveService;

    /**
     * 跳转到文章喜欢首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "articleLove.html";
    }

    /**
     * 跳转到添加文章喜欢
     */
    @RequestMapping("/articleLove_add")
    public String articleLoveAdd() {
        return PREFIX + "articleLove_add.html";
    }

    /**
     * 跳转到修改文章喜欢
     */
    @RequestMapping("/articleLove_update/{articleLoveId}")
    public String articleLoveUpdate(@PathVariable Integer articleLoveId, Model model) {
        ArticleLove articleLove = articleLoveService.selectById(articleLoveId);
        model.addAttribute("item",articleLove);
        LogObjectHolder.me().set(articleLove);
        return PREFIX + "articleLove_edit.html";
    }

    /**
     * 获取文章喜欢列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result<List<ArticleLove>> list(String condition) {
        Result<List<ArticleLove>> result = Result.instance();
        try{
            List<ArticleLove> list = articleLoveService.selectList(null);
            result.setData(list);
        }catch (Exception e){
            result.setError("500","获取文章标签出错");
        }
        return result;
    }

    /**
     * 新增文章喜欢
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result<Boolean> add(ArticleLove articleLove) {
        Result<Boolean> result = Result.instance();
        try{
            articleLoveService.insert(articleLove);
            result.setData(true);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 删除文章喜欢
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(required = true, value = "ids[]") List<Integer> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.articleLoveService.deleteBatchIds(ids);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 修改文章喜欢
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result<Boolean> update(ArticleLove articleLove) {
        Result<Boolean> result = Result.instance();
        try{
            articleLoveService.updateById(articleLove);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 文章喜欢详情
     */
    @RequestMapping(value = "/detail/{articleLoveId}")
    @ResponseBody
    public Result<ArticleLove> detail(@PathVariable("articleLoveId") Integer articleLoveId) {
        Result<ArticleLove> result = Result.instance();
        try{
            ArticleLove articleLove = articleLoveService.selectById(articleLoveId);
            result.setData(articleLove);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }
}
